<template>
  <div class="login-container">
    <div class="box">
      <div class="avatar">
        <img src="../assets/logo.png" alt="">
      </div>
      <!--      登录表单区域-->
      <el-form :model="loginFrom" ref="loginRef" :rules="rules" class="demo-ruleForm">
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user-solid" class="info"
                    v-model="loginFrom.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" prefix-icon="el-icon-lock" class="info"
                    v-model="loginFrom.password" placeholder="请输入用户密码" @keyup.enter.native="submit"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="login" @click="submit">登录</el-button>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="info" size="small" @click="resetFrom">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data () {
    return {
      loginFrom: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 3, max: 8, message: '用户名在3-8个字符之间', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '用户密码不能为空', trigger: 'blur' },
          { min: 6, max: 15, message: '用户名在6-15个字符之间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置表单
    resetFrom () {
      this.$refs.loginRef.resetFields()
    },
    loginState (msg, type) {
      this.$msg({
        message: msg,
        showClose: true,
        type: type,
        duration: 1500
      })
    },
    // 效验登录规则
    submit () {
      this.$refs.loginRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('login', this.loginFrom)
        if (res.meta.status !== 200) {
          return this.loginState('登录失败')
        }
        this.loginState('登录成功', 'success')
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang='less' scoped>
.login-container {
  position: relative;
  background: #2b4b6b;
  height: 100%;

  .box {
    width: 450px;
    height: 340px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #FFFFFF;
    border-radius: 3px;
  }
}

.avatar {
  width: 110px;
  height: 110px;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #FFFFFF;
  border: 1px solid #eeeeee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #eeeeee;
  text-align: center;

  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    border: 1px solid #dcdcdc;
    background: #eeeeee;
    box-sizing: border-box;
  }
}

.demo-ruleForm {
  width: 100%;
  position: absolute;
  bottom: 0;
  padding: 0 30px;
  box-sizing: border-box;

  .btn {
    display: flex;
    justify-content: flex-end;
    height: 30px;
    line-height: 30px;
  }

  .login {
    width: 100%;
    text-align: center;
  }
}
</style>
